package employee;

import user.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class EmployeeMenuFrm extends JFrame {
//    public EmployeeMenuFrm(){
//        setTitle("企业员工管理系统 - 普通员工界面");
//        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        setSize(800,600);
//        setLocationRelativeTo(null);
//
//        JLabel welcomeLabel = new JLabel("欢迎你，普通员工："+ Register.currentUser.getUsername());
//        welcomeLabel.setFont(new Font("微软雅黑",Font.BOLD,18));
//        welcomeLabel.setHorizontalAlignment(SwingConstants.CENTER);
//        getContentPane().add(welcomeLabel,BorderLayout.NORTH);
//


    private User loginUser;  //登录用户信息
    private EmployeeDao employeeDao = new EmployeeDao();
    private JPanel contentPanel;

    public EmployeeMenuFrm(User user) {
        super("员工主界面 -" + user.getUsername());
        this.loginUser = user;
        initUI();
        initMenu();
        showWelcomePage();
        setVisible(true);
    }

    private void initUI(){
        setSize(1000,600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        contentPanel = new JPanel(new BorderLayout(20,20));
        add(contentPanel);
    }

    private void initMenu(){
        JMenuBar menuBar = new JMenuBar();
        JMenu functionMenu = new JMenu("功能");

        //我的信息，所有用户可见
        JMenuItem myInfoItem = new JMenuItem("我的信息");
        myInfoItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showMyInfo();
            }
        });
        functionMenu.add(myInfoItem);

        //管理员功能
        if(loginUser.getRole() == 2){
            JMenuItem empManageItem = new JMenuItem("员工管理");
            empManageItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    showEmployeeManagement();
                }
            });
            functionMenu.add(empManageItem);
        }

        //退出功能
        JMenu exitMenu = new JMenu("系统");
        JMenuItem exitItem = new JMenuItem("退出");
        exitItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        exitMenu.add(exitItem);
        menuBar.add(functionMenu);
        menuBar.add(exitMenu);
        setJMenuBar(menuBar);
    }

    private void showWelcomePage(){
        contentPanel.removeAll();
        JLabel welcomeLabel = new JLabel("欢迎" + loginUser.getUsername()+"!",SwingConstants.CENTER);
        welcomeLabel.setFont(new Font("微软雅黑",Font.BOLD,24));
        contentPanel.add(welcomeLabel,BorderLayout.CENTER);
        contentPanel.revalidate();
    }

    private void showMyInfo(){
        //关联的员工信息
        try {
            //根据用户id查询关联的员工信息
            List<Employee> employees = employeeDao.listEmployeesByUserId(loginUser.getUserId());
            if (employees.isEmpty()){
                JOptionPane.showMessageDialog(this,"暂无您的员工信息");
                return ;
            }
            contentPanel.removeAll();
            Employee emp = employees.get(0);
            JPanel infoPanel = new JPanel(new GridLayout(0,1,10,10));
            infoPanel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));

            infoPanel.add(new JLabel("===个人信息===="));
            infoPanel.add(new JLabel("工号:"+emp.getEmpno()));
            infoPanel.add(new JLabel("姓名:"+emp.getEname()));
            infoPanel.add(new JLabel("岗位:"+emp.getJob()));
            infoPanel.add(new JLabel("入职时间:"+formatDate(emp.getHiredata())));
            infoPanel.add(new JLabel("工资:"+emp.getSal()));
            infoPanel.add(new JLabel("加班费:"+emp.getOfee()));
            infoPanel.add(new JLabel("部门编号:"+emp.getDeptno()));

            contentPanel.add(infoPanel,BorderLayout.CENTER);
            contentPanel.revalidate();
        }catch (SQLException e){
            JOptionPane.showMessageDialog(this,"查询失败"+e.getMessage());
        }
    }

    private void showEmployeeManagement(){
        if (loginUser.getRole() != 2){
            return ;   //非管理员不能访问

        }
        try{
            List<Employee> employees = employeeDao.listEmployees();
            String[] columns = {"工号","姓名","岗位","上级领导","入职时间","工资","加班费","部门编号"};
            Object[][] data = new Object[employees.size()][columns.length];

            for (int i = 0;i < employees.size();i++){
                Employee emp = employees.get(i);
                data[i] = new Object[]{
                        emp.getEmpno(),
                        emp.getEname(),
                        emp.getJob(),
                        emp.getMgr(),
                        formatDate(emp.getHiredata()),
                        emp.getSal(),
                        emp.getOfee(),
                        emp.getDeptno()
                };
            }
            contentPanel.removeAll();
            JTable table = new JTable(data,columns);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
            contentPanel.add(new JScrollPane(table), BorderLayout.CENTER);
            contentPanel.revalidate();
        }catch (SQLException  e){
            JOptionPane.showMessageDialog(this,"加载员工列表失败:"+e.getMessage());
        }
    }

    //日期格式化
    private String formatDate(Date date){
        if (date == null) return "未记录";
        return new java.text.SimpleDateFormat("yyyy-MM-dd").format(date);
    }





}
